#Copyright 2012 Elias Farhan
#
#This file is part of Squirrel.
#
#Squirrel is free software: you can redistribute it and/or modify it under the terms of the GNU General 
#Public License as published by the Free Software Foundation, either version 3 of the License, or (at your 
#option) any later version.
#
#Squirrel is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
#without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
#See the GNU General Public License for more details.
import sys
import lxml.etree as ET
sys.path.insert(0, '..')
from client.game.gameobject import Character, GameObject
from client.game.box import Box
from client.game.level import Level
def char2XML(character):
	root = ET.Element("Character")
	root.attrib["filename"] = character.filename
	root.attrib["pos"] = str(character.pos)
	root.attrib["size"] = str(character.size)
	#root.attrib["statenumber"] = str(character.movement.getNumState())
	child1 = ET.SubElement(root, "State")
	for state in character.movement.state:
		tmp = ET.Element(state.name)
		tmp.attrib["name"] = state.name
		tmp.attrib["speed"] = str(state.speed)
		tmp.attrib["frames"] = str(state.frames)
		tmp.attrib["index"] = str(state.index)
		childtmp = ET.SubElement(tmp, "Box")
		index = 0
		for box in state.boxes:
			boxtmp = ET.Element("Box"+str(index))
			boxtmp.attrib["origin"] = str(box.origin)
			boxtmp.attrib["end"] = str(box.end)
			childtmp.append(boxtmp)
			index+=1
		child1.append(tmp)
	tree = ET.ElementTree(root)
	return tree

def level2XML(level):
	root = ET.Element("Level")
	root.attrib["name"] = level.name
	#root.attrib["statenumber"] = str(character.movement.getNumState())
	child1 = ET.SubElement(root, "GameObject")
	for gameobject in level.gameobjects:
		tmp = ET.Element("name")
		tmp.attrib["filename"] = gameobject.filename
		tmp.attrib["size"] = str(gameobject.size)
		tmp.attrib["pos"] = str(gameobject.pos)
		childtmp = ET.SubElement(tmp, "Box")
		index = 0
		for box in gameobject.boxes:
			boxtmp = ET.Element("Box"+str(index))
			boxtmp.attrib["origin"] = str(box.origin)
			boxtmp.attrib["end"] = str(box.end)
			childtmp.append(boxtmp)
			index+=1
		child1.append(tmp)
	tree = ET.ElementTree(root)
	return tree
